<template>
  <base-breadcrumb>
    <searchData :search-data="searchArr" @search="search" />
    <a-card :bordered="false">
      <!-- <title-name title="列表" /> -->
      <list-table
        ref="table"
        :columns="columns"
        :request="loadData"
        :isShowDeleteCount="false"
        :export-config="{ use: true,select: '日常物资需用计划',all: '日常物资需用计划'}"
        :export-permission="{select: 'mm_037_der_05',all: 'mm_037_der_05'}"
        :statisticsConfig="{use: true,statistics}"
      >
        <template slot="prefix" slot-scope="ref">
          <a-button v-btn-permission="'mm_037_add_01'"
                    type="primary" icon="plus" @click="$nav('/materials/plan/dailyMaterialRequest/add')"
          >新增
          </a-button>
        </template>
        <template slot="index" slot-scope="text, record, index">{{ index + 1 }}</template>
        <template slot="auditStatusStr" slot-scope="text, record, index">
          <list-status :status="record.auditStatus" :text="text" />
        </template>
        <template slot="operation" slot-scope="text, record, index">
          <MoreButton>
            <a
              v-btn-permission="'mm_037_loo_02'"
              @click="$nav('/materials/plan/dailyMaterialRequest/add',
            {id:record.id,detail:1})"
            >
              查看
            </a>
            <a
              v-btn-permission="'mm_037_edi_03'"
              v-if="editStatus(record)"
              @click="$nav('/materials/plan/dailyMaterialRequest/add',{id:record.id})"
            >
              编辑
            </a>
            <a v-btn-permission="'mm_037_del_04'" v-if="record.auditStatus === 'DRAFT' || record.auditStatus === 'REJECTED'"
                  @click="handleDel(record.id)"
            >删除</a>
            <a
              v-if="record.auditStatus === 'PAST' || record.auditStatus === 'SUBMITTED' || record.auditStatus === 'REJECTED'"
              @click="chooseId = record.id;$refs.approval.open()"
            >审批历史</a>
          </MoreButton>
        </template>
      </list-table>
    </a-card>
    <approval
      :id="chooseId"
      ref="approval"
      title="审批"
    />
  </base-breadcrumb>
</template>
<script>
import Api from '@/api/material/plan/dailyMaterialRequest.js'
import SelectSubCompany from '@/components/Select/SelectSubCompany'
import SelectProject from '@/components/Select/SelectProject'
import OrgTreeSelect from '@/components/OrgTreeSelect'
import approval from './components/approval'

export default {
  name: 'a' + Date.now(),
  components: {
    approval
  },
  data() {
    return {
      chooseId: '',
      statistics: {},
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          scopedSlots: { customRender: 'index' }
        },
        {
          title: '所属分公司',
          dataIndex: 'branchComName',
          width: 120
        },
        {
          title: '日常物资需用计划编码',
          dataIndex: 'materialDailyCode',
          width: 220
        },
        {
          title: '项目名称',
          dataIndex: 'projectName',
          width: 200
        },
        {
          title: '编号',
          dataIndex: 'numberAll',
          width: 250
        },
        {
          title: '联系人',
          dataIndex: 'contact',
          width: 100
        },
        {
          title: '电话',
          dataIndex: 'tel',
          width: 120
        },
        {
          title: '计划时间',
          dataIndex: 'planDate',
          width: 120
        },
        {
          title: '创建人',
          dataIndex: 'creatorName',
          width: 120
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          width: 120
        },
        {
          title: '状态',
          dataIndex: 'auditStatusStr',
          scopedSlots: { customRender: 'auditStatusStr' },
          width: 150
        },
        {
          title: '操作',
          width: 200,
          fixed: 'right',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      searchArr: [
        // 搜索框
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        // {
        //   name: '所属分公司',
        //   valueKey: 'branchComId',
        //   type: SelectSubCompany
        // },
        // {
        //   name: '项目名称',
        //   type: SelectProject,
        //   valueKey: 'projectId',
        //   keyValue: 'branchComId'
        // },
        {
          name: '日常物资需用计划编码',
          value: 'materialDailyCode',
          type: 'input'
        },
        {
          name: '编号',
          value: 'number',
          type: 'input'
        },
        {
          name: '联系人',
          value: 'contact',
          type: 'input'
        },
        {
          name: '电话',
          value: 'tel',
          type: 'input'
        },
        {
          name: '创建人',
          value: 'creatorName',
          type: 'input'
        },
        {
          name: '计划时间',
          value: 'planDate',
          type: 'date'
        },
        {
          name: '创建时间',
          value: 'createTime',
          type: 'dateRangeFormat'
        },
        {
          name: '状态',
          value: 'auditStatus',
          type: 'select',
          data: this.STATUS
        }
      ],
      loadData: (params) => {
        let res = Object.assign(params, { query: this.queryParam })
        Api.findListCount(res).then(res => {
          if (res.code === this.SUCCESS_CODE) this.statistics = res.data
        })
        return Api.findListByMain(res)
      },
      queryParam: {},
    }
  },
  created() {
  },
  methods: {
    editStatus(record) {
      if (record.auditStatus !== 'SUBMITTED') {
        return true
      }
      return record.auditStatus === 'CHANGE' && record.changeUserId === this.$userInfo().id
    },
    search(val) {
      this.queryParam = val || {}
      this.$refs.table.getData(null, true)
    },
    handleDel(id) {
      this.$delList2(Api.deleteByMain, () => {
        this.search()
      }, { ids: [id] })
    }
  }
}
</script>
